﻿using RSSDataPump.Types;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RSSDataPump
{
    public static class SqlDB
    {
        public static void insertNews(RSSData rd)
        {
            var connection =  System.Configuration.ConfigurationManager.ConnectionStrings["LocalDB"].ToString();
            using (var conn = new SqlConnection(connection))
            {
                using (var command = new SqlCommand("InsertNewsFeed", conn))
                {
                    if (rd.date == "1/1/0001 12:00:00 AM")
                        rd.date = DateTime.Now.ToShortDateString();
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add(new SqlParameter("@theUrl", rd.source));
                    command.Parameters.Add(new SqlParameter("@theTitle", rd.title));
                    command.Parameters.Add(new SqlParameter("@theSummary", rd.summary));
                    command.Parameters.Add(new SqlParameter("@theRSSUrl", rd.source));
                    command.Parameters.Add(new SqlParameter("@theNewsDate", rd.date));
                    command.Parameters.Add(new SqlParameter("@theCreator", "cm"));

                    conn.Open();
                    try { 
                    command.ExecuteNonQuery();
                        }
                    catch(Exception e)
                    {
                        var t = true;
                    }
                    conn.Close();
                }
            }
        }
        public static List<Authors> GetNewsAuthors()
        {
            List<Authors> newsAuthors = new List<Authors>();
           
            var connection = System.Configuration.ConfigurationManager.ConnectionStrings["LocalDB"].ToString();
            using (var conn = new SqlConnection(connection))
            {
                using (var command = new SqlCommand("GetAllAuthors", conn))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    conn.Open();

                    SqlDataReader test = command.ExecuteReader();
                    while (test.Read())
                    {
                        Authors a = new Authors();
                        a.Weight = Convert.ToInt32(test["Weight"]);
                        a.Url = Convert.ToString(test["Url"]);
                        newsAuthors.Add(a);
                    }
                    conn.Close();
                }
                return newsAuthors;
            }
        }
    }
}
